package com.example.excel.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.excel.R;

public class DragView extends FrameLayout {

    private int mLastX;
    private int mLastY;
    private int mParentWidth;
    private int mParentHeight;

    View childAt;

    public DragView(@NonNull Context context) {
        super(context);
    }

    public DragView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DragView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    protected void initView() {
        childAt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ViewGroup mViewGroup = (ViewGroup) getParent();
                if (null != mViewGroup && mParentWidth == 0) {
                    mParentWidth = mViewGroup.getWidth();
                    mParentHeight = mViewGroup.getHeight();
                }
                int ea = event.getAction();
                switch (ea) {
                    case MotionEvent.ACTION_DOWN:
                        mLastX = (int) event.getRawX();//获取触摸事件触摸位置的原始X坐标
                        mLastY = (int) event.getRawY();
                    case MotionEvent.ACTION_MOVE:
                        //event.getRawX();获得移动的位置
                        int dx = (int) event.getRawX() - mLastX;
                        int dy = (int) event.getRawY() - mLastY;
                        if(childAt != null){
                            updateLayout(childAt, dx, dy);
                        }
                        mLastX = (int) event.getRawX();
                        mLastY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:

                        break;
                }
                return true;
            }
        });
    }

    private void updateLayout(View view, int dx, int dy) {
        int l = view.getLeft() + dx;
        int b = view.getBottom() + dy;
        int r = view.getRight() + dx;
        int t = view.getTop() + dy;

        //下面判断移动是否超出屏幕
        if (l < 0) {
            l = 0;
            r = l + view.getWidth();
        }
        if (t < 0) {
            t = 0;
            b = t + view.getHeight();
        }
        if (r > mParentWidth) {
            r = mParentWidth;
            l = r - view.getWidth();
        }
        if (b > mParentHeight) {
            b = mParentHeight;
            t = b - view.getHeight();
        }
        view.layout(l, t, r, b);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
       /* if (childAt == null) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childView = getChildAt(i);
                if (childView instanceof ImageView) {
                    this.childAt = childView;
                    initView();
                    return;
                }
            }
        }*/

//        childAt  = findViewById(R.id.iv_tag);
        initView();
    }
}

